#pragma once
#ifndef __GLF_DEBUGGER_NETWORK_H__
#define __GLF_DEBUGGER_NETWORK_H__

#include <glf/config.h>

#if GLF_ENABLE_DEBUGGER

#include <glf/debugger/debugger.h>
#include <vector>

namespace glf
{
namespace debugger
{

class Network : public Module
{
	GLF_DECLARE_SINGLETON_WITH_DEP(Network, Debugger)
public:
	enum EventType
	{
		EVENT_UNKNOWN,
		EVENT_LOG,
		EVENT_PACKET_SENT,
		EVENT_PACKET_RECEIVED,
		
		EVENT_COUNT
	};

	struct Event
	{
		Event(EventType type = EVENT_UNKNOWN, uint64 id = 0, const char* text = "") :
			mType(type),
			mId(id),
			mText(text),
			mData(NULL),
			mDataSize(0)
		{
		}

		EventType mType;
		uint64 mId;
		std::string mText;
		const void* mData;
		uint mDataSize;
	};

	class Decoder
	{
	public:
		virtual ~Decoder() {}
		virtual std::string ToString(const Event& event) = 0;
	};
	static void AddEvent(const Event& event);

	virtual ~Network();

	virtual void UpdateFrame();
	virtual void Parse(int type, PacketReader& in);
	virtual void ConnectionClosed();

private:
	Network();
	

	enum MessageType
	{
		// Debugger to Glf
		MESSAGE_SET_ENABLED,

		// Glf to Debugger
		MESSAGE_EVENTS
	};

	struct PendingEvent
	{
		EventType mType;
		uint64 mId;
		std::string mText;
		uint64 mTimeStamp;
		std::vector<char*, DebuggerAllocator<char*> > mData;
	};

	void SendEvents();

	bool mEnabled;
	std::vector<PendingEvent, DebuggerAllocator<PendingEvent> > mPendingEvents;
};

} // end namespace debugger
} // end namespace glf

#endif // GLF_ENABLE_DEBUGGER

#endif // __GLF_DEBUGGER_NETWORK_H__
